package owl2vowl.converter;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;

public class IRIConverter extends AbstractConverter {
	protected IRI mainOntology;
	protected Collection<IRI> depdencyOntologies;
	private static final Logger logger = LogManager.getLogger(IRIConverter.class);

	public IRIConverter(IRI ontologyIRI) {
		this(ontologyIRI, Collections.emptyList());
	}

	public IRIConverter(IRI ontologyIRI, Collection<IRI> necessaryExternals) {
		this.mainOntology = ontologyIRI;
		this.depdencyOntologies = Collections.unmodifiableCollection(necessaryExternals);
	}

	protected void loadOntology() throws OWLOntologyCreationException {
		logger.info("Converting IRIs...");
		logger.info("Loading ontologies ... [" + this.mainOntology + ",  " + this.depdencyOntologies + "]");
		this.manager = OWLManager.createOWLOntologyManager();
		if (!this.depdencyOntologies.isEmpty()) {
			Iterator logOntoName = this.depdencyOntologies.iterator();

			while (logOntoName.hasNext()) {
				IRI externalIRI = (IRI) logOntoName.next();
				this.manager.loadOntology(externalIRI);
			}

			logger.info("External ontologies loaded!");
		}

		this.ontology = this.manager.loadOntology(this.mainOntology);
		this.loadedOntologyPath = this.mainOntology.toString();
		String logOntoName1;
		if (!this.ontology.isAnonymous()) {
			logOntoName1 = ((IRI) this.ontology.getOntologyID().getOntologyIRI().get()).toString();
		} else {
			logOntoName1 = this.mainOntology.toString();
			logger.info("Ontology IRI is anonymous. Use loaded URI/IRI instead.");
		}

		logger.info("Ontologies loaded! Main Ontology: " + logOntoName1);
	}
}